<?php

class Admin_FaqController extends Zend_Controller_Action {

    public function init() {
        if (!Zend_Auth::getInstance()->hasIdentity()) {
            $this->_redirect("/admin/login");
        }
        $this->_helper->layout->setLayout("admin");
    }

    public function indexAction() {
        $db = new Admin_Model_Faq();
        $fetchAll = $db->fetchAll();
        $this->view->allQues = $fetchAll;
    }

    public function addAction() {
        $form = new Admin_Form_Faq();
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                $db = new Admin_Model_Faq();
                $db->createFAQ(array(
                    'question' => $values['que'],
                    'answer' => $values['ans'],
                    'enabled' => $values['enabled']
                ));
                $this->_redirect("/admin/faq");
            }
        }
    }

    public function editAction() {
        $id = $this->getinputidAction();
        $db = new Admin_Model_Faq();
        $byId = $db->getById($id);
        $form = new Admin_Form_Faq();
        $form->que->setValue($byId[0]['question']);
        $form->ans->setValue($byId[0]['answer']);
        $form->enabled->setValue($byId[0]['enabled']);
        $form->id->setValue($byId[0]['id']);
        $form->submit->setLabel("Update Question");
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                $db = new Admin_Model_Faq();
                $db->updateFAQ(array(
                    'question' => $values['que'],
                    'answer' => $values['ans'],
                    'enabled' => $values['enabled']
                        ), $values['id']);
                $this->_redirect("/admin/faq");
            }
        }
    }

    /**
     * Get ID.    
     */
    public function getinputidAction() {
        $filters = array(
            'id' => array('HtmlEntities', 'StripTags', 'StringTrim')
        );
        $validators = array(
            'id' => array('Int')
        );
        $input = new Zend_Filter_Input($filters, $validators);
        $input->setData($this->getRequest()->getParams());
        $id = $input->id; // id
        return $id;
    }

}

